package com.share.user.command;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.share.user.domain.User;
import org.springframework.web.client.RestTemplate;

/**
 * created by caifan 2018/06/15
 */
public class OrderServiceCommand extends HystrixCommand<User> {
    private RestTemplate restTemplate;
    private Long id;

    public OrderServiceCommand(String commandGroupKey, RestTemplate restTemplate, Long id) {
        super(HystrixCommandGroupKey.Factory.asKey(commandGroupKey));
        this.restTemplate = restTemplate;
        this.id = id;
    }

    /**
     * 服务调用
     * @return
     * @throws Exception
     */
    @Override
    protected User run() throws Exception {
        System.out.println(Thread.currentThread().getName());
        return restTemplate.getForObject("http://microservice-provider-user:7010/rest/user/" + id, User.class);
    }

    @Override
    protected User getFallback() {
        User user = new User();
        user.setAge(88);
        user.setEmail("system@163.com");
        user.setNickname("hello");
        return user;
    }
}
